﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  BaseUserRepositoryTest 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  :  
 * Created Time :  2013/11/1 22:44:22
 * Update Man   :  Joey Liang
 * Update Time  :  2013/11/1 22:44:22
************************************************************************/

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.QualityTools.Testing.Fakes;
using IdleTest.EFAndMVCDemo.Models;
using IdleTest.EFAndMVCDemo.IRepositories;
using System.Data.Entity;
using IdleTest.EFAndMVCDemo.Repositories;
using IdleTest.EFAndMVCDemo.Repositories.Fakes;
using System.Collections.Generic;
using IdleTest.MSTest;
using System.Linq;
using System.Data.Entity.Fakes;
using IdleTest.EFAndMVCDemo.TestUtilities;

namespace IdleTest.EFAndMVCDemo.RepositoryTest
{
    public abstract class BaseUserRepositoryTest : RepositoryTest<UserModel, string>
    {
        protected UserTestHelper UserTestHelper = new UserTestHelper();

        protected abstract IUserRepository UserRepository { get;}

        protected IList<UserModel> ExistedUsers;

        [TestInitialize]
        public virtual void Init()
        {
            this.ExistedUsers = new List<UserModel> { UserTestHelper.ExistedUser };
        }

        public override void GetSingleTest()
        {
            base.GetSingleTest();
            
            AssertCommon.IsNull<string, UserModel>(
                TestCommon.GetEmptyStrings(), true, p => UserRepository.GetSingle(p));
            AssertCommon.IsNull(true, UserRepository.GetSingle(UserTestHelper.NotExistedLoginName));

            UserModel actual = UserRepository.GetSingle(UserTestHelper.ExistedLoginName);
            UserTestHelper.AssertEqual(UserTestHelper.ExistedUser, actual);
        }

        public override void InsertTest()
        {
            base.InsertTest();
                        
            //验证添加成功的场景
            //密码与他人相同也可添加
            UserModel register1 = new UserModel { LoginName = "register1", Password = UserTestHelper.ExistedPassword };
            UserModel register2 = UserTestHelper.NotExistedUser;
            AssertCommon.ThrowException<UserModel>(
                new UserModel[] { register1, register2 }, false, p => UserRepository.Insert(p));

            //获取用户且应与注册的信息保持一致
            UserModel actualRegister1 = UserRepository.GetSingle(register1.LoginName);
            UserTestHelper.AssertEqual(register1, actualRegister1);

            UserModel actualRegister2 = UserRepository.GetSingle(register2.LoginName);
            UserTestHelper.AssertEqual(register2, actualRegister2);

            //验证添加失败的场景，使用ThrowException来验证添加
            AssertCommon.ThrowException<UserModel>(
                new UserModel[] { 
                    register1,  //不能重复添加
                    //由于LoginName对应数据库字段为主键，故不能为空
                    new UserModel { LoginName = string.Empty, Password = UserTestHelper.NotExistedPassword }, 
                }, true, p => UserRepository.Insert(p));
        }
    }
}
